let roleId,roleName;
layui.config({
	version:'1510164124181'
}).use(['form', 'laypage', 'layer', 'element'],function(){
	form = layui.form;
	layer = layui.layer;
	$ = layui.jquery;
	laypage = layui.laypage;//分页
	element = layui.element; //元素操作
	token=getCookieValue("token");
	roleId = GetQueryString("roleId");
	SelectMeauInfo();
	if(!isNotNull(roleId)){
		roleId=0;
	}else{
		SelectMeauById();
	}
});

function SelectMeauById() {
	var index = top.layer.msg('加载中，请稍候',{icon: 16,time:false,shade:0.8});
	$.ajax({
		url: httpurl+"/SelectRoleById",
		cache: false,
		data: {
			"roleid":roleId,
		},
		headers: {
			"Access-Control-Allow-Origin": "*",
			"Access-Control-Allow-Methods": "*",
			"Access-Control-Allow-Headers": "Authorization",
			"Authorization": GetTokenInfo()
		},
		contentType : 'application/json',
		success: function (data) {
			// console.log(JSON.stringify(data));
			top.layer.closeAll();
			roleName=data.data.RoleInfo.roleName;
			$("#roleName").val(roleName);
			if(data.rtnCode==200){
				let dataInfo=data.data.roleMenus;
				for(let i=0;i<dataInfo.length;i++){
					$("#AllChooseList").find("input[type='checkbox'][value="+dataInfo[i].menu.id+"]").attr("checked","checked");
				}
			}

		}
	});
}

function SelectMeauInfo() {
	$.ajax({
		url: httpurl+"/callAllMenuUser",
		cache: false,
		async:false,
		headers: {
			"Access-Control-Allow-Origin": "*",
			"Access-Control-Allow-Methods": "*",
			"Access-Control-Allow-Headers": "Authorization",
			"Authorization": GetTokenInfo()
		},
		contentType : 'application/json',
		data: {
			"token":token,
		},
		success: function (data) {
			// console.log(JSON.stringify(data));
			let info="";
			if(data.rtnCode==200){
				let dataInfo=data.data;
				for(let i=0;i<dataInfo.length;i++) {
					info+='<li class="margin_left2b"><label class="check-box"><input type="checkbox" name="parentList" onclick="parentListFun(this)" value="'+dataInfo[i].tempid+'"/><span>'+dataInfo[i].menu_name+'</span></label></li>';
					let ChildInfo=dataInfo[i].subdirectory;
					if(ChildInfo!=null){
						info+='<li  class="margin_left10b">';
						for(let a=0;a<ChildInfo.length;a++){
							info+='<span><label class="check-box"><input type="checkbox" name="childrenList" onclick="childrenListFun(this)" value="'+ChildInfo[a].id+'"/><span>'+ChildInfo[a].menu_name+'</span></label></span>';
						}
						info+="</li>";
					}
				}

			}
			$("#AllChooseList").html(info);
		}
	});
}

function sel(){
	let str="";
	$("#AllChooseList").find("input[type='checkbox']").each(function () {
		if($(this).is(':checked')){
			str+=$(this).val()+",";
		}
	})

	if(str.length>0){
		str=str.substring(0,str.length-1);
	}
	let roleName = $("#roleName").val();
	if(roleName==""){
		layer.msg("请填写角色名称");
		return;
	}
	let httpAjax="updateRole";
	if(roleId==0){
		httpAjax="insertRole";
	}
	$.ajax({
		type: "POST",
		url: httpurl + "/"+httpAjax,
		cache: false,
		data: JSON.stringify({
			"id":roleId,
			"roleName":roleName,
			"roleList":str
		}),
		headers: {
			"Access-Control-Allow-Origin": "*",
			"Access-Control-Allow-Methods": "*",
			"Access-Control-Allow-Headers": "Authorization",
			"Authorization": GetTokenInfo()
		},
		contentType : 'application/json',
		success: function (data) {
			console.log(JSON.stringify(data));
			if(data.rtnCode==200){
				layer.msg("添加成功!!");
				re();
			}
		},error:function (data) {
			layer.msg("读取接口失败,请稍后再试试");
		}
	})
}
function re() {
	window.location.href="RoleList.html";
}
